package com.agileai.bpm.controller.ui;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;

import com.agileai.bpm.common.BPMConstants.EmbedVariable;
import com.agileai.bpm.domain.define.ProcessVariable;
import com.agileai.bpm.manager.PropertiesResourceManager;
import com.agileai.bpm.model.DataFieldsDocument.DataFields;
import com.agileai.bpm.model.ProcessDefineDocument.ProcessDefine;
import com.agileai.bpm.model.VariableType;
import com.agileai.domain.DataParam;
import com.agileai.hotweb.domain.FormSelect;
import com.agileai.hotweb.renders.AjaxRenderer;
import com.agileai.hotweb.renders.ViewRenderer;

public class VariableBox {
	public static final String SPLITER_TAG = "!!--!!";
	
	public static final String VARIABLE_TYPE = "_variableType";
	public static final String VARIABLE_CODE = "_variableCode";
	
	public static final String system_var = "system_var";
	public static final String process_var = "process_var";
	public static final String preset_code = "preset_code";
	public static final String global_var = "global_var";
	
	private FormSelect varibleType = new FormSelect();
	private HashMap<String,FormSelect> variables = new HashMap<String,FormSelect>();
	private String defaultVarType = system_var;
	
	private ProcessDefine processDefine = null;
	private DataParam param = null;
	
	private String boxVarType = null;
	
	private enum BoxVarTypes {
		full,primitive,variable
	};
	
	public VariableBox(ProcessDefine processDefine,DataParam param){
		this.processDefine = processDefine;
		this.param = param;
		this.boxVarType = param.get("boxVarType");
		this.initContent();
	}
	private void initContent(){
		varibleType.putValue(system_var, "系统变量");
		varibleType.putValue(process_var, "流程变量");
		varibleType.putValue(preset_code, "预设编码");
		varibleType.putValue(global_var, "全局变量");
		
		FormSelect systemVar = new FormSelect();
		systemVar.putValue("${"+EmbedVariable.bizRecordId+"}", "业务记录标识");
		systemVar.putValue("${"+EmbedVariable.processId+"}", "流程定义标识");
		systemVar.putValue("${"+EmbedVariable.processInstId+"}", "流程实例标识");
		systemVar.putValue("${"+EmbedVariable.activityCode+"}", "活动节点编码");
		systemVar.putValue("${"+EmbedVariable.submiter+"}", "流程发起人名");
		systemVar.putValue("&WFP_ID=@{__processId__}&WFIP_ID=@{__processInstId__}&WFA_CODE=@{__activityCode__}&WFIP_BUSINESS_ID=@{__bizRecordId__}","电脑端参数集");
		systemVar.putValue("/@{__processId__}/@{__processInstId__}/@{__activityCode__}/@{__bizRecordId__}","移动端参数集");
		
		variables.put(system_var, systemVar);
		
		FormSelect presetCode = new FormSelect();
		
		presetCode.putValue(EmbedVariable.targetUserCodes, "目标用户编码");
		presetCode.putValue(EmbedVariable.targetUserIds, "目标用户标识");
		variables.put(preset_code, presetCode);
		
		FormSelect gloabalVar = new FormSelect();
		
		PropertiesResourceManager propertiesResourceManager = PropertiesResourceManager.instance();		
		HashMap<String,LinkedHashMap<String,String>> bpmconfigs  = propertiesResourceManager.getPropertiesConfig();
		Iterator<String> codeKeys = bpmconfigs.keySet().iterator();
		while (codeKeys.hasNext()){
			String codeKey = codeKeys.next();
			HashMap<String,String> vars = bpmconfigs.get(codeKey);
			Iterator<String> varKeys = vars.keySet().iterator();
			while (varKeys.hasNext()){
				String varKey = varKeys.next();
				
				String fullKey = PropertiesResourceManager.BPMConfigsKey + "." + codeKey + "." + varKey; 
//				String value = vars.get(varKey);
				
				gloabalVar.putValue("${"+fullKey+"}",fullKey);
			}
		}
		
		
		variables.put(global_var, gloabalVar);
		
		this.initProcessVariables();
	}
	private void initProcessVariables(){
		DataFields dataFields = processDefine.getDataFields();
		if (dataFields != null){
			VariableType[] variableTypes = dataFields.getDataFieldArray();
			FormSelect processVar = new FormSelect();
			this.initVariableTypes(variableTypes, processVar, process_var);
		}
	}
	private void initVariableTypes(VariableType[] variableTypes,FormSelect varFormSelect,String variablesType){
		if (variableTypes != null){
			for (int i=0;i < variableTypes.length;i++){
				VariableType variableType = variableTypes[i];
				String code = variableType.getCode();
				String dataType = variableType.getDataType().toString();
				if (ProcessVariable.VarType.datasetobject.equals(dataType)
					|| ProcessVariable.VarType.soapobject.equals(dataType)
					|| ProcessVariable.VarType.xmlobject.equals(dataType)){
					continue;
				}

				if (this.boxVarType.equals(BoxVarTypes.primitive) 
						|| this.boxVarType.equals(BoxVarTypes.variable)){
					if (ProcessVariable.VarType.mapobject.equals(dataType)){
						continue;
					}
				}
				
				String dataTypeDesc = ProcessVariable.VarTypeMap.get(dataType);		
				varFormSelect.putValue(code+","+dataType, code+" ( "+dataTypeDesc+" )");
			}
			variables.put(variablesType, varFormSelect);
		}
	}

	public FormSelect getVarTypeFormSelect(){
		return this.varibleType;
	}
	
	public FormSelect getVariableFormSelect(String code){
		if (variables.containsKey(code)){
			return variables.get(code);	
		}
		else{
			return new FormSelect();
		}
	}
	
	private String getDefaultVarType() {
		return defaultVarType;
	}
	
	public void setDefaultVarType(String defaultVarType) {
		this.defaultVarType = defaultVarType;
	}
	
	public ViewRenderer doShowVariableBoxAction(){
		String rspText = "";
		String code = this.getDefaultVarType();
		FormSelect varTypeFormSelect = this.getVarTypeFormSelect();
		varTypeFormSelect.setHasBlankValue(false);
		varTypeFormSelect.setSelectedValue(code);
		FormSelect variableFormSelect = this.getVariableFormSelect(code);
		variableFormSelect.setHasBlankValue(false);
		rspText = varTypeFormSelect.getSyntax() + VariableBox.SPLITER_TAG 
			+ variableFormSelect.getSyntax();
		return new AjaxRenderer(rspText);
	}
	public ViewRenderer doChangeVariableTypeAction(){
		String rspText = "";
		String varType = param.get("_variableType");
		FormSelect variableFormSelect = this.getVariableFormSelect(varType);
		variableFormSelect.setHasBlankValue(false);
		rspText = variableFormSelect.getSyntax();		
		return new AjaxRenderer(rspText);
	}
	public ViewRenderer doSelectVariableAction(){
		String rspText = "";
		String varType = param.get("_variableType");
		String varCode = param.get("_variableCode");
		if (VariableBox.system_var.equals(varType)
				|| VariableBox.preset_code.equals(varType)
				|| VariableBox.global_var.equals(varType)
			){
			rspText = varCode;
		}
		else{
			String[] varCodeArray = varCode.split(",");
			String realCode = varCodeArray[0];
			String dataType = varCodeArray[1];
			
			if (BoxVarTypes.variable.equals(this.boxVarType)){
				if (ProcessVariable.VarType.string.equals(dataType)
						|| ProcessVariable.VarType.number.equals(dataType)){
					rspText = realCode;
				}
			}
			else{
				if (ProcessVariable.VarType.string.equals(dataType)
					|| ProcessVariable.VarType.number.equals(dataType)){
					rspText = "${"+realCode+"}";
				}
				else if (ProcessVariable.VarType.mapobject.equals(dataType)){
					rspText = "${"+realCode+".SomeFieldName}";
				}
			}
		}
		return  new AjaxRenderer(rspText);
	}
	
	public ViewRenderer doSelectVariableCode(){
		String rspText = "";
		String varType = param.get("_variableType");
		String varCode = param.get("_variableCode");
		if (VariableBox.system_var.equals(varType)
				|| VariableBox.preset_code.equals(varType)
				|| VariableBox.global_var.equals(varType)
				){
			if (varCode.startsWith("${") && varCode.endsWith("}")){
				rspText = varCode.substring(2,varCode.length()-1);
			}else{
				rspText = varCode;				
			}
		}
		else{
			String[] varCodeArray = varCode.split(",");
			String realCode = varCodeArray[0];
//			String dataType = varCodeArray[1];
			rspText = realCode;
		}
		return  new AjaxRenderer(rspText);
	}	
}
